package com.mybatis.demo.demo1;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.mybatis.util.MyBatisSqlSessionUtil;

public class StudentsDAOImpl implements StudentsDAO {

	SqlSession sqlSession = MyBatisSqlSessionUtil.getSqlSessionFactory().openSession();
	
	@Override
	public Integer add(Students students) {
		int iid = sqlSession.insert("com.mybatis.demo.demo1.StudentsMapper.addStudent", students);
		sqlSession.commit();
		sqlSession.close();
		return iid;
	}

	@Override
	public Students find(Integer iid) {
		Students student = sqlSession.selectOne("com.mybatis.demo.demo1.StudentsMapper.findStudents", iid);
		sqlSession.close();
		return student;
	}

	@Override
	public Integer modify(Students students) {
		int updateIid = sqlSession.update("com.mybatis.demo.demo1.StudentsMapper.updateStudent", students);
		sqlSession.commit();
		sqlSession.close();
		return updateIid;
	}

	@Override
	public Integer delete(Integer iid) {
		int delNum = sqlSession.delete("com.mybatis.demo.demo1.StudentsMapper.removeStudent", iid);
		sqlSession.commit();
		sqlSession.close();
		return delNum;
	}

	@Override
	public Integer addMulti(List<Students> studentsList) {
		int num = sqlSession.insert("com.mybatis.demo.demo1.StudentsMapper.addMultiStudent", studentsList);
		sqlSession.commit();
		sqlSession.close();
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> findStudents1() {
		
		List<Map<String, Object>> mapList =  sqlSession.selectList("com.mybatis.demo.demo1.StudentsMapper.findStudents1", Map.class);
		sqlSession.close();
		return mapList;
	}

	@Override
	public List<Students> findStudents2() {
		List<Students> studentsList =  sqlSession.selectList("com.mybatis.demo.demo1.StudentsMapper.findstudents2", Students.class);
		sqlSession.close();
		return studentsList;
	}
	
	
	
	

}
